import moment from "moment";
import { useSystemrStore } from "../stores/system";
import { getConfigurationTrainType } from "../utils/utils";

const trainMove = (mapBuild: any, fieldMap: any) => {
  const systemStore = useSystemrStore();
  const trainMap: any = {};
  const station = {};
  const fieldMapTmp = fieldMap;
  const lineGpsMap = {};
  const moveFrequency = 500;
  //  parseInt(
  //    seajs.buffer.func.getSystemConfigParameters("trainMoveFrequency")
  //  );
  const changeFrequency = 1000;
  //  parseInt(
  //    seajs.buffer.func.getSystemConfigParameters(
  //      "singleTrainReceiveSignalFrequency"
  //    )
  //  ) * 1000;
  const size = new (window as any).SuperMap.Size(22, 16);
  const offset = new (window as any).SuperMap.Pixel(-(size.w / 2), -size.h / 2);
  const myIconleve = new (window as any).SuperMap.Icon(
    "../asstes/images/superMap/car_level0.png",
    size,
    offset
  );
  const myIconleve0 = new (window as any).SuperMap.Icon(
    "../asstes/images/superMap/momalLabel.png",
    size,
    offset
  );
  const myIconleve1 = new (window as any).SuperMap.Icon(
    "../asstes/images/superMap/car_level1.png",
    size,
    offset
  );
  const myIconleve2 = new (window as any).SuperMap.Icon(
    "../asstes/images/superMap/car_level2.png",
    size,
    offset
  );
  const myIconleve3 = new (window as any).SuperMap.Icon(
    "../asstes/images/superMap/car_level3.png",
    size,
    offset
  );
  if (mapBuild.map) {
    mapBuild.map.events.on({
      movestart: (object: any) => {
        if (object && object.zoomChanged) {
          if (Object.keys(trainMap).length) {
            for (let key in trainMap) {
              trainMap[key].move(trainMap[key].point);
            }
          }
        }
      },
    }); //添加click事件
  }

  function resetMarkPoint(trainMark: any) {
    const trainMessage = trainMark.trainMessage;
    if (trainMessage.offLineTime >= 15000) {
      clearInterval(trainMark.setIntervalId);
      trainMark.markerDestroy();
      delete trainMap[trainMessage.trainNo];
      return;
    }
    trainMessage.offLineTime = trainMessage.offLineTime + moveFrequency;
    let step = getStep(trainMessage.speed, moveFrequency);
    //如果移动的距离大于可移动的距离，移动距离被设置为可移动距离。并更新可移动距离的值。
    if (step >= trainMessage.moveLenth) {
      step = trainMessage.moveLenth;
      trainMessage.moveLenth = 0;
    } else {
      trainMessage.moveLenth = trainMessage.moveLenth - step;
    }
    const lineGpsChild = trainMessage.lineGps[0] || [];
    const trainPosition = trainMark.point;
    let gpsInfo = trainMessage.gpsInfo;
    let endLength = 0;
    //如果线路上下一个分割点为空值，这将其设置为当前站。
    if (gpsInfo == null) {
      gpsInfo = trainMessage.station;
      trainMessage.gpsInfo = gpsInfo;
      changePreAndAfterPlace(trainMessage, lineGpsChild, step);
    }
    let nextPoint = mapBuild.bd09_To_Gcj02(
      trainMessage.nextStation.lng,
      trainMessage.nextStation.lat
    );
    if (step <= 0) {
      //当速度为零时将车移动到下一个站点
      if (trainMessage.station.moveType == 1) {
        nextPoint = mapBuild.bd09_To_Gcj02(
          trainMessage.station.lng,
          trainMessage.station.lat
        );
      }
      if (step <= 0 && trainMessage.speedCount <= 5) {
        if (trainMark.isRotation) {
          const prePosition = trainMessage.preGpsInfo;
          const afterPosition = trainMessage.afterGpsInfo;
          trainMark.setRotation(afterPosition, prePosition, trainMark);
        }
        trainMark.move(trainPosition);
      } else if (nextPoint.x && nextPoint.y) {
        if (trainMessage.station.moveType == 1) {
          trainMessage.gpsInfo = trainMessage.station;
        } else {
          trainMessage.gpsInfo = trainMessage.nextStation;
        }
        changePreAndAfterPlace(trainMessage, lineGpsChild, step);
        if (trainMark.isRotation) {
          const prePosition = trainMessage.preGpsInfo;
          const afterPosition = trainMessage.afterGpsInfo;
          trainMark.setRotation(afterPosition, prePosition, trainMark);
        }
        trainMark.move(nextPoint);
      }
    } else if (step <= 0 && trainMessage.moveLenth <= 0) {
      if (trainPosition.x && trainPosition.y) {
        if (trainMark.isRotation) {
          const prePosition = trainMessage.preGpsInfo;
          const afterPosition = trainMessage.afterGpsInfo;
          trainMark.setRotation(afterPosition, prePosition, trainMark);
        }
        trainMark.move(trainPosition);
      }
    } else {
      //获取下一个位置点
      const gpsInfoPoint = mapBuild.bd09_To_Gcj02(gpsInfo.lng, gpsInfo.lat);
      let nextPosition = getNextGps(trainPosition, gpsInfoPoint, step);
      if (
        trainPosition.x != gpsInfoPoint.x &&
        trainPosition.y != gpsInfoPoint.y
      ) {
        const lonLatA = new new (window as any).SuperMap.LonLat(
          trainPosition.x,
          trainPosition.y
        )();
        const lonLatB = new new (window as any).SuperMap.LonLat(
          gpsInfoPoint.x,
          gpsInfoPoint.y
        )();
        endLength =
          new (window as any).SuperMap.Util.distVincenty(lonLatA, lonLatB) *
          1000;
      }
      if (trainMessage.station.sequence < trainMessage.nextStation.sequence) {
        if (
          !(
            trainMessage.station.sequence < gpsInfo.sequence &&
            gpsInfo.sequence <= trainMessage.nextStation.sequence
          )
        ) {
          if (gpsInfo.sequence >= trainMessage.nextStation.sequence) {
            trainMessage.gpsInfo = trainMessage.nextStation;
          } else if (trainMessage.station.sequence >= gpsInfo.sequence) {
            trainMessage.gpsInfo = trainMessage.station;
          }
        }
      } else {
        if (
          !(
            trainMessage.station.sequence > gpsInfo.sequence &&
            gpsInfo.sequence >= trainMessage.nextStation.sequence
          )
        ) {
          if (gpsInfo.sequence <= trainMessage.nextStation.sequence) {
            trainMessage.gpsInfo = trainMessage.nextStation;
          } else if (trainMessage.station.sequence <= gpsInfo.sequence) {
            trainMessage.gpsInfo = trainMessage.station;
          }
        }
      }
      if (endLength <= step && !gpsInfo.isSite) {
        if (lineGpsChild[0].pathType == 2) {
        } else {
          for (let index = 0; index < lineGpsChild.length; index++) {
            let item = lineGpsChild[index];
            if (
              trainMessage.station.sequence < trainMessage.nextStation.sequence
            ) {
              if (item.sequence == gpsInfo.sequence + 1) {
                gpsInfo = item;
                if (
                  trainMessage.station.sequence < gpsInfo.sequence &&
                  gpsInfo.sequence <= trainMessage.nextStation.sequence
                ) {
                  trainMessage.gpsInfo = item;
                } else if (trainMessage.station.sequence >= gpsInfo.sequence) {
                  trainMessage.gpsInfo = trainMessage.station;
                } else {
                  trainMessage.gpsInfo = trainMessage.nextStation;
                }
                changePreAndAfterPlace(trainMessage, lineGpsChild, step);
                return false;
              }
            } else if (
              trainMessage.station.sequence > trainMessage.nextStation.sequence
            ) {
              if (item.sequence == gpsInfo.sequence - 1) {
                gpsInfo = item;
                if (
                  trainMessage.station.sequence > gpsInfo.sequence &&
                  gpsInfo.sequence >= trainMessage.nextStation.sequence
                ) {
                  trainMessage.gpsInfo = item;
                } else if (trainMessage.station.sequence <= gpsInfo.sequence) {
                  trainMessage.gpsInfo = trainMessage.station;
                } else {
                  trainMessage.gpsInfo = trainMessage.nextStation;
                }
                changePreAndAfterPlace(trainMessage, lineGpsChild, step);
                return false;
              }
            } else {
              // 若sequence相同，或没有找到适合的gps，则设为站点
              trainMessage.gpsInfo = trainMessage.station;
              changePreAndAfterPlace(trainMessage, lineGpsChild, step);
              return false;
            }
          }
        }
        nextPosition = getNextGps(
          nextPosition,
          mapBuild.bd09_To_Gcj02(gpsInfo.lng, gpsInfo.lat),
          step - endLength
        );
      } else if (endLength <= step && gpsInfo.isSite) {
        if (gpsInfo.siteCode == trainMessage.nextStation.siteCode) {
          nextPoint = mapBuild.bd09_To_Gcj02(
            trainMessage.nextStation.lng,
            trainMessage.nextStation.lat
          );
          if (nextPoint.x && nextPoint.y) {
            trainMessage.gpsInfo = trainMessage.nextStation;
            changePreAndAfterPlace(trainMessage, lineGpsChild, step);
            if (trainMark.isRotation) {
              const prePosition = trainMessage.preGpsInfo;
              const afterPosition = trainMessage.afterGpsInfo;
              trainMark.setRotation(afterPosition, prePosition, trainMark);
            }
            trainMark.move(nextPoint);
          }
          return;
        }
        if (lineGpsChild[0].pathType == 2) {
        } else {
          for (let key in lineGpsChild) {
            const item = lineGpsChild[key];
            if (
              trainMessage.station.sequence < trainMessage.nextStation.sequence
            ) {
              if (item.sequence == gpsInfo.sequence + 1) {
                gpsInfo = item;
                if (
                  trainMessage.station.sequence < gpsInfo.sequence &&
                  gpsInfo.sequence <= trainMessage.nextStation.sequence
                ) {
                  trainMessage.gpsInfo = item;
                } else if (trainMessage.station.sequence >= gpsInfo.sequence) {
                  trainMessage.gpsInfo = trainMessage.station;
                } else {
                  trainMessage.gpsInfo = trainMessage.nextStation;
                }
                changePreAndAfterPlace(trainMessage, lineGpsChild, step);
                return false;
              }
            } else if (
              trainMessage.station.sequence > trainMessage.nextStation.sequence
            ) {
              if (item.sequence == gpsInfo.sequence - 1) {
                gpsInfo = item;
                if (
                  trainMessage.station.sequence > gpsInfo.sequence &&
                  gpsInfo.sequence >= trainMessage.nextStation.sequence
                ) {
                  trainMessage.gpsInfo = item;
                } else if (trainMessage.station.sequence <= gpsInfo.sequence) {
                  trainMessage.gpsInfo = trainMessage.station;
                } else {
                  trainMessage.gpsInfo = trainMessage.nextStation;
                }
                changePreAndAfterPlace(trainMessage, lineGpsChild, step);
                return false;
              }
            } else {
              trainMessage.gpsInfo = trainMessage.station;
              changePreAndAfterPlace(trainMessage, lineGpsChild, step);
              return false;
            }
          }
        }
      }
      nextPoint = new (window as any).SuperMap.Geometry.Point(
        nextPosition.x,
        nextPosition.y
      );
      if (nextPoint.x && nextPoint.y) {
        if (trainMark.isRotation) {
          const prePosition = trainMessage.preGpsInfo;
          const afterPosition = trainMessage.afterGpsInfo;
          trainMark.setRotation(afterPosition, prePosition, trainMark);
        }
        trainMark.move(nextPoint);
      }
    }
  }
  function getNextGps(pointA: any, pointB: any, step: number) {
    let latStep = 0;
    let lngStep = 0;
    let distance = 0;
    let percentage = 0;
    const gpsObject: any = {};
    if (pointA.x != pointB.x && pointA.y != pointB.y) {
      const lonLatA = new (window as any).SuperMap.LonLat(pointA.x, pointA.y);
      const lonLatB = new (window as any).SuperMap.LonLat(pointB.x, pointB.y);
      distance =
        (window as any).SuperMap.Util.distVincenty(lonLatA, lonLatB) * 1000;
    }
    if (distance > 0) {
      if (step < distance) {
        percentage = step / distance;
        lngStep = (pointA.x - pointB.x) * percentage;
        latStep = (pointA.y - pointB.y) * percentage;
        gpsObject.x = pointA.x - lngStep;
        gpsObject.y = pointA.y - latStep;
      } else {
        gpsObject.x = pointB.x;
        gpsObject.y = pointB.y;
      }
    } else {
      gpsObject.x = pointB.x;
      gpsObject.y = pointB.y;
    }
    return gpsObject;
  }
  function changePreAndAfterPlace(
    trainMessage: any,
    gpsPlaceList: any,
    step: number
  ) {
    if (Object.keys(trainMessage).length && gpsPlaceList.length) {
      const gpsInfo = trainMessage.gpsInfo;
      const endPlace = trainMessage.endSiteInfo;
      for (let index = 0; index < gpsPlaceList.length; index++) {
        let item = gpsPlaceList[index];
        if (gpsInfo.sequence == item.sequence) {
          if (index == 0) {
            // 若到第一个点，速度还不等于0，则保持原来的方向。
            if (step != 0) {
              trainMessage.preGpsInfo = gpsPlaceList[1];
              trainMessage.afterGpsInfo = gpsPlaceList[0];
            } else {
              // 若速度为0，则接下来是要朝正方向开
              trainMessage.preGpsInfo = gpsPlaceList[0];
              trainMessage.afterGpsInfo = gpsPlaceList[1];
            }
            return false;
          } else if (index == gpsPlaceList.length - 1) {
            // 若走到了最后一个个点，速度还不等于0，则保持原来的方向。
            if (step != 0) {
              trainMessage.preGpsInfo = gpsPlaceList[index - 1];
              trainMessage.afterGpsInfo = gpsPlaceList[index];
            } else {
              // 若速度为0，则接下来是要朝反方向开
              trainMessage.preGpsInfo = gpsPlaceList[index];
              trainMessage.afterGpsInfo = gpsPlaceList[index - 1];
            }
            return false;
          } else {
            if (gpsInfo.sequence < endPlace.sequence) {
              trainMessage.preGpsInfo = gpsPlaceList[index - 1];
              trainMessage.afterGpsInfo = item;
              return false;
            } else if (gpsInfo.sequence > endPlace.sequence) {
              trainMessage.preGpsInfo = gpsPlaceList[index + 1];
              trainMessage.afterGpsInfo = item;
              return false;
            }
          }
        }
      }
    }
  }
  function getStep(speed: number, millisecond: number) {
    const speedTmp = speed / 3600;
    return speedTmp * millisecond;
  }
  function refreshWebSocket(
    realData: any,
    type: string,
    singlePoint: any,
    gpsInfoTmp: any,
    from: string
  ) {
    const trainNo = realData.trainNo;
    const trainType = realData.trainType;
    const line = realData.lineNo;
    const gpsKey = line + "_" + trainType;
    const trainInfo = realData.data;
    let trainMessage: any = {}
    let station: any = {};
    let nextStation: any = {};
    let endSiteInfo: any = {};
    let gpsInfo: any = {};
    const fieldMap = fieldMapTmp[trainType];
    let trainMark = trainMap[trainNo];
    let stations = JSON.parse(sessionStorage.getItem("station") as string)[
      gpsKey
    ];
    let lineGps = JSON.parse(sessionStorage.getItem("lineGpsMap") as string)[
      gpsKey
    ];
    if (Object.keys(stations).length == 0 || Object.keys(lineGps).length == 0) {
      getConfigurationTrainType(line, trainType);
      stations = systemStore.station[gpsKey];
      lineGps = systemStore.lineGpsMap[gpsKey];
      if (
        Object.keys(stations).length == 0 ||
        Object.keys(lineGps).length == 0
      ) {
        return;
      }
    }
    if (fieldMap) {
      if (
        !(
          trainInfo[fieldMap["currentSite"]] &&
          trainInfo[fieldMap["nextSite"]] &&
          trainInfo[fieldMap["speed"]] &&
          trainInfo[fieldMap["endSite"]]
        )
      ) {
        return;
      }
      if (!trainMark) {
        const lineGpsChild = lineGps[0] || [];
        const tmpCurrentSite = trainInfo[fieldMap["currentSite"]];
        const tmpNextSite = trainInfo[fieldMap["nextSite"]];
        if (stations[0].moveType == 1) {
          const tmpCurStation1 = stations[0];
          const tmpNexStation1 = stations[1];
          const tmpCurStation2 = stations[stations.length - 1];
          const tmpNexStation2 = stations[stations.length - 2];
          //获取当前站信息
          if (
            checkSiteCode(tmpCurStation2.siteCode, tmpCurrentSite) &&
            checkSiteCode(tmpNexStation2.siteCode, tmpNextSite)
          ) {
            station = tmpCurStation2;
          } else if (
            checkSiteCode(tmpCurStation1.siteCode, tmpCurrentSite) &&
            checkSiteCode(tmpNexStation1.siteCode, tmpNextSite)
          ) {
            station = tmpCurStation1;
          }
          //获取下一站信息
          if (
            checkSiteCode(tmpNexStation2.siteCode, tmpCurrentSite) &&
            checkSiteCode(tmpCurStation2.siteCode, tmpNextSite)
          ) {
            nextStation = tmpCurStation2;
          } else if (
            checkSiteCode(tmpNexStation1.siteCode, tmpCurrentSite) &&
            checkSiteCode(tmpCurStation1.siteCode, tmpNextSite)
          ) {
            nextStation = tmpCurStation1;
          }
        }
        for (let index = 0; index < stations.length; index++) {
          //获取当前站信息
          if (Object.keys(station).length == 0) {
            if (
              checkSiteCode(
                stations[index].siteCode,
                trainInfo[fieldMap["currentSite"]]
              )
            ) {
              station = stations[index];
            }
          }
          //获取下一站信息
          if (Object.keys(nextStation).length == 0) {
            if (
              checkSiteCode(
                stations[index].siteCode,
                trainInfo[fieldMap["nextSite"]]
              )
            ) {
              nextStation = stations[index];
            }
          }
          if (Object.keys(station).length && Object.keys(nextStation).length) {
            return false;
          }
        }
        for (let index = 0; index < stations.length; index++) {
          //获取终点站信息
          if (Object.keys(endSiteInfo).length == 0) {
            if (
              checkSiteCode(
                stations[index].siteCode,
                trainInfo[fieldMap["endSite"]]
              )
            ) {
              if (index == 0) {
                if (station.sequence > nextStation.sequence) {
                  endSiteInfo = stations[index];
                  return false;
                }
              } else {
                endSiteInfo = stations[index];
                return false;
              }
            }
          }
        }
        //获取下一个移动点信息
        if (lineGpsChild[0].pathType == 2) {
          //TODO
        } else {
          for (let index = 0; index < lineGpsChild.length; index++) {
            let item = lineGpsChild[index];
            //获取下一个gps点，如果下一站的序列大于当前站，则取比当前序列大一的gps.
            if (station.sequence < nextStation.sequence) {
              if (item.sequence == station.sequence + 1) {
                if (
                  station.sequence < item.sequence &&
                  item.sequence <= nextStation.sequence
                ) {
                  gpsInfo = item;
                } else {
                  gpsInfo = lineGpsChild[index + 1];
                }
                return false;
              }
              //获取下一个gps点，如果下一站的序列小于当前站，则取比当前序列小一的gps.
            } else if (station.sequence > nextStation.sequence) {
              if (item.sequence == station.sequence - 1) {
                if (
                  station.sequence > item.sequence &&
                  item.sequence >= nextStation.sequence
                ) {
                  gpsInfo = item;
                } else {
                  gpsInfo = lineGpsChild[index - 1];
                }
                return false;
              }
              //获取下一个gps点，如果下一站的序列等于当前站，则取当前站的gps.
            } else {
              if (item.sequence == station.sequence) {
                if (index == 0) {
                  gpsInfo = lineGpsChild[1];
                } else if (index == lineGpsChild.length - 1) {
                  gpsInfo = lineGpsChild[lineGpsChild.length - 2];
                } else {
                  if (station.sequence < endSiteInfo.sequence) {
                    gpsInfo = lineGpsChild[index + 1];
                  } else {
                    gpsInfo = lineGpsChild[index - 1];
                  }
                }
                return false;
              }
            }
          }
        }

        function openInfoWin() {
          let trainInfo: any = {};
          let faultInfo: any = {};
          const thisMark = (window as any).this;
          if (thisMark && thisMark.infowin) {
            thisMark.infowin.destroy();
            thisMark.infowin = null;
          } else if (thisMark) {
            const trainMessage = thisMark.trainMessage;
            faultInfo = getFaultInfo(trainMessage.faults); // 取等级最大（3最大），时间最新的一条故障
            let changeClass = "none";
            if (trainMessage.faults.length > 0) {
              changeClass = "";
            }
            let showOp = systemStore.showOp;
            trainInfo.trainNo = trainNo;
            trainInfo.trainType = trainType;
            trainInfo.lineId = line;
            const tmpLonLat = thisMark.point;
            trainInfo.position = new (window as any).SuperMap.Geometry.Point(
              tmpLonLat.x,
              tmpLonLat.y
            );
            trainInfo.gpsInfo = trainMessage.gpsInfo;
            const markLevel = thisMark.faultLevel || thisMark.alertLevel || 0;
            let tmpHtml = `<div class="markWindow markWindow${markLevel}">
                                <span class="markWindowChange glyphicon glyphicon-refresh ${
                                  showOp ? "none" : changeClass
                                }"></span>
                                <div class="changeDiv" data-type="train"><p>列车号：<span>${trainNo}</span></p>
                                <p>速度：<span id="${trainNo}speed">${
              trainMessage.trainInfo[fieldMap["speed"]] || 0
            }</span>&nbsp;km/h</p>`;
            if (type != "singleTrain") {
              if (!trainMessage.nextStation["siteName"]) {
                // console.log(trainMessage.nextStation["siteName"])
                trainMessage.nextStation["siteName"] = "-";
              }
              tmpHtml += `<p>主风压力：<span id="${trainNo}windPressure">${
                trainMessage.trainInfo[fieldMap["windPressure"]] || 0
              }</span>bar</p>
                                    <p>下一站：<span id="${trainNo}nextSite">${
                trainMessage.nextStation["siteName"]
              }</span></p>
                                     </div><div class="changeDiv" data-type="fault" style="display: none;"><p>${
                                       faultInfo.fault_name
                                     }</p><p>故障代码：${faultInfo.fault_code}
                                    </p><p>故障系统：${
                                      faultInfo.fault_system
                                    }</p><p>故障时间：${moment(
                faultInfo.fault_time
              ).format("HH:mm:ss")}
                                    </p></div><div class="buttonLike ${
                                      showOp ? "none" : ""
                                    }" ><span class="button_Info" >查看详情</span></div>
                                    <div class='triangle'></div></div>`;
            } else {
              tmpHtml = tmpHtml + "<div class='triangle'></div></div>";
            }
            var infoPopupName = "info" + trainType + trainNo;
            thisMark.createInfowin(infoPopupName, tmpHtml);
          }
          (function (trainInfo, faultInfo) {
            // $(".button_Info")
            //   .off("click")
            //   .click(function () {
            //     let type = $(".markWindow .changeDiv:visible").attr(
            //       "data-type"
            //     );
            //     if (type === "train") {
            //       seajs.buffer.trainInfo = trainInfo;
            //       mining.utils.commonLog("", "", "查看详情", {
            //         data: trainInfo,
            //         url: "/realTimeMonitor/staticMonitor.html",
            //       });
            //       seajs.buffer.func.expgetPage(
            //         "realTimeMonitor/staticMonitor.html"
            //       );
            //     } else {
            //       $(".shadeDiv").show();
            //       common.jumpFaultReview(trainInfo, faultInfo);
            //     }
            //   });
            // $(".markWindowChange")
            //   .off("click")
            //   .click(function () {
            //     $(this)
            //       .closest(".markWindow")
            //       .find(".changeDiv")
            //       .toggle("fast");
            //   });
          })(trainInfo, faultInfo);
        }

        // 从所有故障信息中取级别最高时间最新的一条故障
        function getFaultInfo(faults: any) {
          let faultInfo: any = {};
          for (let item of faults) {
            // 如果已有faultInfo，则进行比较
            if (faultInfo.fault_alias) {
              // 如果当前对象的级别比之前取的那条高，或同级别下当前对象的时间比之前取得那条新
              if (
                item.fault_level > faultInfo.fault_level ||
                (item.fault_level == faultInfo.fault_level &&
                  item.fault_time > faultInfo.fault_time)
              ) {
                faultInfo = item;
              }
            } else {
              faultInfo = item;
            }
          }
          return faultInfo;
        }

        // const trainMessage: any = {};
        trainMessage.endSiteInfo = endSiteInfo;
        trainMessage.afterGpsInfo = gpsInfo;
        trainMessage.trainInfo = trainInfo;
        trainMessage.preGpsInfo = station;
        trainMessage.station = station;
        trainMessage.nextStation = nextStation;
        trainMessage.endSiteInfo = endSiteInfo;
        trainMessage.lineNo = line;
        trainMessage.trainType = trainType;
        trainMessage.trainNo = trainNo;
        trainMessage.offLineTime = 0;
        trainMessage.speedCount = 0;
        //速度，需要除以100
        trainMessage.speed = trainInfo[fieldMap["speed"]] || 0;
        trainMessage.moveLenth =
          getStep(trainMessage.speed, changeFrequency) || 0;
        trainMessage.lineGps = lineGps;
        //获取结束站点的信息
        trainMessage.endSite = trainInfo[fieldMap["endSite"]];
        if (Object.keys(trainMessage.station).length) {
          const faultLevel = getMaxFaultLevel(realData.faults);
          let tmpIcon;
          if (faultLevel == 0) {
            if (Object.keys(realData.faults).length == 0) {
              tmpIcon = myIconleve;
            } else {
              tmpIcon = myIconleve0;
            }
          } else if (faultLevel == 1) {
            tmpIcon = myIconleve1;
          } else if (faultLevel == 2) {
            tmpIcon = myIconleve2;
          } else {
            tmpIcon = myIconleve3;
          }
          if (
            singlePoint &&
            singlePoint.x &&
            singlePoint.y &&
            Object.keys(gpsInfoTmp).length
          ) {
            const pointTmp = new (window as any).SuperMap.Geometry.Point(
              singlePoint.x,
              singlePoint.y
            );
            // trainMark = new MyMarker(
            //   mapBuild.map,
            //   mapBuild.markers,
            //   pointTmp,
            //   openInfoWin,
            //   type,
            //   tmpIcon
            // );
            trainMessage.gpsInfo = gpsInfoTmp;
          } else {
            const pointTmp = mapBuild.bd09_To_Gcj02(station.lng, station.lat);
            // trainMark = new MyMarker(
            //   mapBuild.map,
            //   mapBuild.markers,
            //   pointTmp,
            //   openInfoWin,
            //   type,
            //   tmpIcon
            // );
            trainMessage.gpsInfo = gpsInfo;
          }
          trainMessage.faults = realData.faults;
          trainMark.trainMessage = trainMessage;
          trainMark.timestamp = trainInfo.timestamp;
          trainMap[trainNo] = trainMark;

          const iconPopupName = "icon" + trainType + trainNo;
          trainMark.createIconwin(iconPopupName);
          const setIntervalId = setInterval(function () {
            resetMarkPoint(trainMark);
          }, moveFrequency);
          trainMark.setIntervalId = setIntervalId;
          systemStore.setIntervalArr.push(setIntervalId);
        }
      } else {
        //如果标志存在
        //更新站点信息
        if (
          from !== "historyServer" &&
          trainMark.timestamp &&
          parseInt(trainMark.timestamp) >= parseInt(trainInfo.timestamp)
        ) {
          return false;
        }
        trainMessage = trainMark.trainMessage;
        if (
          !checkSiteCode(
            trainMessage.station.siteCode,
            trainInfo[fieldMap["currentSite"]]
          ) ||
          !checkSiteCode(
            trainMessage.nextStation.siteCode,
            trainInfo[fieldMap["nextSite"]]
          )
        ) {
          const tmpCurrentSite = trainInfo[fieldMap["currentSite"]];
          const tmpNextSite = trainInfo[fieldMap["nextSite"]];
          //获取当前站信息
          if (stations[0].moveType == 1) {
            const tmpCurStation1 = stations[0];
            const tmpNexStation1 = stations[1];
            const tmpCurStation2 = stations[stations.length - 1];
            const tmpNexStation2 = stations[stations.length - 2];
            //获取当前站信息
            if (
              checkSiteCode(tmpCurStation2.siteCode, tmpCurrentSite) &&
              checkSiteCode(tmpNexStation2.siteCode, tmpNextSite)
            ) {
              station = tmpCurStation2;
            } else if (
              checkSiteCode(tmpCurStation1.siteCode, tmpCurrentSite) &&
              checkSiteCode(tmpNexStation1.siteCode, tmpNextSite)
            ) {
              station = tmpCurStation1;
            }
            //获取下一站信息
            if (
              checkSiteCode(tmpNexStation2.siteCode, tmpCurrentSite) &&
              checkSiteCode(tmpCurStation2.siteCode, tmpNextSite)
            ) {
              nextStation = tmpCurStation2;
            } else if (
              checkSiteCode(tmpNexStation1.siteCode, tmpCurrentSite) &&
              checkSiteCode(tmpCurStation1.siteCode, tmpNextSite)
            ) {
              nextStation = tmpCurStation1;
            }
          }
          if (
            Object.keys(station).length == 0 ||
            Object.keys(nextStation).length == 0
          ) {
            for (let index = 0; index < stations.length; index++) {
              let item = stations[index];
              //获取当前站信息
              if (Object.keys(station).length == 0) {
                if (
                  checkSiteCode(
                    item.siteCode,
                    trainInfo[fieldMap["currentSite"]]
                  )
                ) {
                  station = item;
                }
              }
              //获取下一站信息
              if (Object.keys(nextStation).length == 0) {
                if (
                  checkSiteCode(item.siteCode, trainInfo[fieldMap["nextSite"]])
                ) {
                  nextStation = item;
                }
              }
            }
          }
          for (let index = 0; index < stations.length; index++) {
            let item = stations[index];
            //获取终点站信息
            if (Object.keys(endSiteInfo).length == 0) {
              if (
                checkSiteCode(item.siteCode, trainInfo[fieldMap["endSite"]])
              ) {
                endSiteInfo = item;
                trainMessage.endSiteInfo = endSiteInfo;
                break;
              }
            }
          }
          if (Object.keys(station).length) {
            trainMessage.station = station;
          }
          if (Object.keys(nextStation).length) {
            trainMessage.nextStation = nextStation;
          }
          const tmpGpsInfo = trainMessage.gpsInfo;
          if (
            tmpGpsInfo &&
            tmpGpsInfo.siteCode != station.siteCode &&
            tmpGpsInfo.siteCode != nextStation.siteCode
          ) {
            const movePoint = mapBuild.bd09_To_Gcj02(station.lng, station.lat);
            if (movePoint.x && movePoint.y) {
              trainMark.move(movePoint);
              trainMessage.gpsInfo = station;
            }
          }
        }
        if (trainMessage.endSite != trainInfo[fieldMap["endSite"]]) {
          trainMessage.endSite = trainInfo[fieldMap["endSite"]];
        }
        //修改速度信息
        trainMessage.speed = trainInfo[fieldMap["speed"]] || 0;
        if (trainMessage.speed <= 0) {
          trainMessage.speedCount = trainMessage.speedCount + 1;
        } else {
          trainMessage.speedCount = 0;
        }
        trainMessage.offLineTime = 0;
        const stepLenth = getStep(trainMessage.speed, changeFrequency) || 0;
        trainMessage.moveLenth = trainMessage.moveLenth + stepLenth;
        trainMessage.trainInfo = trainInfo;
        trainMark.trainMessage = trainMessage;
        trainMark.timestamp = trainInfo.timestamp;
        changeSignalValue(
          trainNo,
          trainInfo,
          fieldMap,
          trainMessage.nextStation["siteName"]
        );
      }
      if (type == "singleTrain" && Object.keys(trainMark).length) {
        mapBuild.map.panTo(
          new (window as any).SuperMap.LonLat(trainMark.point.x, trainMark.point.y)
        );
        trainMessage = trainMark.trainMessage;
        (document.getElementsByClassName("currentStation")[0] as any).innerHTML(trainMessage.station["siteName"]);
        (document.getElementsByClassName("nextStation")[0] as any).innerHTML(trainMessage.nextStation["siteName"]);
        (document.getElementsByClassName("endStation")[0] as any).innerHTML(trainMessage.endSiteInfo["siteName"]);
      }
      // 05C01 没有当前站点信息，但是实时位置需要显示下一站信息
      if (
        type === "singleTrain" &&
        trainMessage.trainType === "05C01" &&
        systemStore.nowPage === "staticMonitor"
      ) {
        (document.getElementsByClassName("currentStation")[0] as any).parentNode.classList.add("hide");
        (document.getElementsByClassName("nextStation")[0] as any).innerHTML(trainMessage.nextStation["siteName"]);
        (document.getElementsByClassName("endStation")[0] as any).parentNode.classList.add("hide");
      }
    }
  }
  function checkSiteCode(siteCodePool: string, siteCode: string) {
    let isCurrentSite = false;
    if (siteCodePool && siteCode) {
      const codePool = siteCodePool.split(/,/);
      if (codePool.length > 0) {
        for (let i = 0; i < codePool.length; i++) {
          if (codePool[i] == siteCode) {
            isCurrentSite = true;
            break;
          }
        }
      }
    }
    return isCurrentSite;
  }
  function getMaxFaultLevel(faults: any) {
    let faultLevel = 0;
    if (faults) {
      for (let i = 0; i < faults.length; i++) {
        if (faults[i]["fault_level"] > faultLevel) {
          faultLevel = faults[i]["fault_level"];
        }
      }
    }
    return faultLevel;
  }
  function changeSignalValue(trainNo:string, trainInfo:any, fieldMap:any, nextSite:string) {
    if (fieldMap) {
      let speedDom :any= document.getElementById(trainNo + "speed");
      if (speedDom) {
        speedDom.innerText‌(trainInfo[fieldMap["speed"]] || 0);
      }
      let windDom:any=document.getElementById(trainNo + "windPressure")
      if (windDom) {
        windDom.innerText‌(
          trainInfo[fieldMap["windPressure"]] || 0
        );
      }
      let nextSiteDom:any=document.getElementById(trainNo + "nextSite")
      if (nextSiteDom) {
        nextSiteDom.innerText‌(nextSite);
      }
    }
  }
  return {
    refreshWebSocket
  }
};
export default trainMove;
